const CryptoJS = require('crypto-js');


/**
 * AES加密 ：字符串 key iv  返回base64
 */
// page--页码
function Encrypt(page, pageSize,keyStr, ivStr) {
    var arr = new Array();
    var arr1 = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    var nonceStr = '';
    for (var i = 0; i < 10; i++) {
        var n = Math.floor(Math.random() * 10);
        arr[i] = arr1[n];
        nonceStr += arr1[n];
    }

    let word = '{"despatchprovinceSelect":"","despatchcitySelect":"","deliveryprovinceSelect":"","deliverycitySelect":"","despatchStart":"","despatchEnd":"","weightBottom":"","weightTop":"","cargoName":"","volumeBottom":"","volumeTop":"","moneyBottom":"","moneyTop":"","presetFlag":false,"orderModel":"","orderType":"","nowPage":'+ page +',"pageSize":' + pageSize + ',"uuid":"' + nonceStr + '","timestamp":' + new Date().getTime() + ',"uuUserId":null}';
    let key = CryptoJS.enc.Utf8.parse('zczyL2d9DfhLZO0z');
    let iv = CryptoJS.enc.Utf8.parse('412SFA342442zczy');

    if (keyStr) {
        key = CryptoJS.enc.Utf8.parse(keyStr);
        iv = CryptoJS.enc.Utf8.parse(ivStr);
    }

    let srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
console.log("加密",Encrypt(1))



/**
 * AES 解密 ：字符串 key iv  返回base64
 *
 */
function Decrypt(word, keyStr, ivStr) {
    let key = CryptoJS.enc.Utf8.parse('zczyL2d9DfhLZO0z');
    let iv = CryptoJS.enc.Utf8.parse('412SFA342442zczy');

    if (keyStr) {
        key = CryptoJS.enc.Utf8.parse(keyStr);
        iv = CryptoJS.enc.Utf8.parse(ivStr);
    }

    let base64 = CryptoJS.enc.Base64.parse(word);
    let src = CryptoJS.enc.Base64.stringify(base64);

    var decrypt = CryptoJS.AES.decrypt(src, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });

    var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}


function getPublicQueryData(number) {
    var arr = new Array();
    var arr1 = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    var nonceStr = '';
    for (var i = 0; i < number; i++) {
        var n = Math.floor(Math.random() * 10);
        arr[i] = arr1[n];
        nonceStr += arr1[n];
    }
    return {
        uuid: nonceStr,
        timestamp: new Date().getTime() + '',
        uuUserId: localStorage.getItem('uuUserId')
    };
}